<?php
define('IN_ADMINCP', TRUE);
define('IN_TESTCP', TRUE);
define('TESTCP_AJAX', AJAX);
define('TESTCP_PATH', dirname(__FILE__));
define('APP_PATH', dirname(TESTCP_PATH));
//echo TESTCP_PATH . '--' . APP_PATH;die;
require APP_PATH . '/app.php';
$admincp = new admincp;
$account = new admincp_account;
//上面的代码采用杨坤的框架，下面的代码采用原生PHP
include "center/Model.class.php";
include "center/Route.class.php";
class Index extends Model{
    public $show='';
    public function __construct(){
        //用户权限认证
    }
    public function Action(){
        //删除1条数据
        if(isset($_POST['id'])){
            $this->oneDelete();
            return;
        }
        //删除多条数据
        if(isset($_POST['deletes'])){
            if($_POST['deletes']=='all'){
                $this->allDelete();
                return;
            }
        }
        //页面出事数据
        $this->show=$this->Show();
    }
    public function Show(){
        $getPage=isset($_GET['page'])? $_GET['page']: 1;
        $dataPage=$this->Page('oa_download',17,$getPage);
        return array(
            'dataPage'=>$dataPage,
            'info'=>'删除分为单个删除和批量删除，删除后不可恢复请谨慎操作！',
            'title'=>'文档列表',
        );
    }
    private function oneDelete(){
        $id=$_POST['id'];
        $sql="DELETE FROM oa_download WHERE id=$id";
        parent::aud($sql);
    }
    public function allDelete(){
        $data="(".implode(',',$_POST['data']).")";
        $sql="DELETE FROM oa_download WHERE id IN $data";
        echo parent::aud($sql);
        exit;
    }
    //from:表单  num:每一页显示数据条数  page:当前页码
    public function Page($from,$num,$page){
        //一共可以分多少页面
        $numSql="SELECT COUNT(*) FROM $from";
        $numPage=ceil((parent::total($numSql))/$num);
        //查询请求页面的数据
        $limit=(($page-1)*$num).','.$num;
        $thisSql="SELECT * FROM $from ORDER BY id DESC LIMIT $limit";
        $thisPage=parent::all($thisSql);
        return array(
            'numPage'=>$numPage,//可以分多少也
            'thisPage'=>$thisPage,//当前分页数据
            'page'=>$page,//当前分页
            'num'=>$num,
            );
    }
}
$model=new Index();
$model->Action();
?>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>后台管理</title>
    <link rel="stylesheet" type="text/css" href="assets/css/public.css"/>
    <link rel="stylesheet" type="text/css" href="assets/css/list.css"/>
    <script type="text/javascript" src="assets/js/jquery-1.9.1.min.js"></script>
</head>
<body>
<div id="content">
    <div id="content-box">
        <div class="content-head">
            <div class="keyboard padding-l40">
                <input type="text" class="search padding-l40 color-black" placeholder="站内搜索"/>
                <div class="keyboard-botton">
                    <a href="javascript:void(0);"><img src="assets/backend-img/content-head/settings.png"/></a>
                    <a href="javascript:void(0);"><img src="assets/backend-img/content-head/language.png"/></a>
                    <a href="javascript:void(0);" class="information"><img src="assets/backend-img/content-head/information.png"/></a>
                    <a href="javascript:void(0);" class="on-off"><img src="assets/backend-img/content-head/on-off.png"/></a>
                </div>
            </div>
            <div class="navigation padding-l40">
                <a href="javascript:void(0);">文档管理</a><a href="javascript:void(0);" class="rating"><?= $model->show['title'];?></a><span><?= $model->show['info'];?></span>
            </div>
        </div>
        <div class="content">
            <div class="box-head">
                <span class="width4">排序</span>
                <span class="width18">标题</span>
                <span class="width12">作者</span>
                <span class="width12">创建时间</span>
                <span class="width14">操作</span>
                <span class="width4 float-r texe-l">批量</span>
            </div>
            <ul id="list">
                <?php if($model->show['dataPage']):?>
                    <?php foreach($model->show['dataPage']['thisPage']as $k=>$v):?>
                        <li>
                            <p class="width4"><?= ($model->show['dataPage']['page']-1)*$model->show['dataPage']['num']+$k+1; ?></p>
                            <p class="width18"><?= $v['name'];?></p>
                            <p class="width12 red"><?= $v['admin'];?></p>
                            <p class="width12"><?= date('Y-m-d',$v['create_tim'])?></p>
                            <p class="width14">
                                <a class="delete-one" href="javascript:void(0);" name="<?= $v['id']?>"><img src="assets/backend-img/content/delete-one.png"/></a>
                                <a class="update-one" href="<?= Route::Url('update',['updateid'=>$v['id']]);?>"><img src="assets/backend-img/content/update-one.png"/></a>
                            </p>
                            <p class="width4 checkbox checkbox-off float-r">
                                <input type="hidden" name="<?= $v['id']; ?>" value="0">
                            </p>
                        </li>
                    <?php endforeach;?>
                <?php endif;?>
            </ul>
            <div class="box-footer">
                <ul class="page float-l">
                    <li><a class="page-l"></a></li>
                    <?php for($i=1;$i<=$model->show['dataPage']['numPage'];$i++):?>
                        <?php if($i==$model->show['dataPage']['page']):?>
                            <li><a href="<?= Route::Url('index',['page'=>$i]);?>" class="page-n page-click"><?= $i;?></a></li>
                        <?php else:?>
                            <li><a href="<?= Route::Url('index',['page'=>$i]);?>" class="page-n"><?= $i;?></a></li>
                        <?php endif;?>
                    <?php endfor;?>
                    <li><a class="page-r"></a></li>
                </ul>
                <div class="box-footer-button">
                    <input type="submit" id="delete-menu" class="delete-menu" value=""/>
                    <span class="red hint" style="float:right; padding:25px 10px 0 0;"></span>
                </div>
            </div>
        </div>
    </div>
</div>
<?php include "menu.php"?>
<script type="text/javascript">
    //复选框点击样式
    var inputs=new Array();
    var id;
    $('.checkbox').on('click',function(){
        a=$(this).children('input').val();
        if(a==0){
            $(this).addClass('checkbox-on');
            $(this).removeClass('checkbox-off');
            $(this).children('input').val(1);
            id=$(this).children('input').attr('name');
        }else{
            $(this).addClass('checkbox-off');
            $(this).removeClass('checkbox-on');
            $(this).children('input').val(0);
            id=$(this).children('input').attr('name');
        }
        if($.inArray(id,inputs)!=-1){
            inputs.splice($.inArray(id,inputs),1);
            console.log(inputs)
        }else{
            inputs.unshift(id);
            console.log(inputs)
        }
    });
    $('.delete-one').click(function(){
            var id=$(this).attr('name');
            $.ajax({
                type:'post',
                data:{id:id},
                url:'<?= Route::Url('index')?>',
                success:function(response,status,xhr){
                    window.location.reload();
                    //console.log(response);
                }
            });
        }
    );
    $('#delete-menu').click(function(){
            if(inputs.length==0){
                $('.hint').html('至少选择一条数据!');
                return;
            }
            $.ajax({
                type:'post',
                data:{data:inputs,deletes:'all'},
                url:'<?= Route::Url('index')?>',
                success:function(response,status,xhr){
                    window.location.reload();
                    //console.log(response);
                }
            });
        }
    );
</script>
</body>
</html>